package com.example.onlinetaxi.view.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.onlinetaxi.BaiDuMap.RoutePlan.RoutePlan;
import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.dto.OrderDto.OrderDTO;
import com.example.onlinetaxi.ui.activity.RouteInfoPage.RouteInfoActivity;
import com.example.onlinetaxi.util.TimeUtil;

import java.text.DecimalFormat;
import java.util.Date;

public class ReceiveOrderDialog extends Dialog {

    private ReceiveOrderDialog(Context context) {
        super(context);
    }

    private ReceiveOrderDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private ReceiveOrderDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static void showInfoDialog(Context context,
                                      OrderDTO orderDTO,
                                      View.OnClickListener cancelOnClick,
                                      View.OnClickListener confirmOnClick,
                                      View.OnClickListener changeOrderOnClick,
                                      View.OnClickListener routeInfoOnClickListener) {
        Builder builder = new Builder(context);

        builder.setTextView(orderDTO);
        builder.setButtonCancel(cancelOnClick);
        builder.setButtonConfirm(confirmOnClick);
        builder.setButtonChangeOrder(changeOrderOnClick);
        builder.setRouteInfoBtn(routeInfoOnClickListener);

        ReceiveOrderDialog customDialog = builder.create();
        customDialog.show();
    }

    /* Builder */
    public static class Builder {
        // 上下文
        private Context context;
        // 文字框
        private TextView distanceToDriverTv;
        private TextView driverProfitTv;
        private TextView useCarTime;
        private TextView useCarType;
        private TextView startInfo;
        private TextView endInfo;
        private TextView orderTime;
        private TextView orderDistance;
        private LinearLayout changeOrder;
        private LinearLayout driverOrderRouteInfo;
        // 箭头
        private ImageView driverArrow;
        // 取消按钮
        private ImageView btnCancel;
        private Button btnConfirm;

        private View mLayout;
        private View.OnClickListener mButtonCancelClickListener;
        private View.OnClickListener mButtonConfirmClickListener;
        private View.OnClickListener changeOrderOnClickListener;
        private View.OnClickListener routeInfoOnClickListener;

        private ReceiveOrderDialog mDialog;

        public Builder(Context context) {
            this.context = context;

            mDialog = new ReceiveOrderDialog(context, R.style.custom_dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 加载布局文件
            mLayout = inflater.inflate(R.layout.dialog_receive_order, null, false);
            // 添加布局文件到 Dialog
            mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            // 文本框初始化
            distanceToDriverTv = mLayout.findViewById(R.id.distance_to_driver);
            driverProfitTv = mLayout.findViewById(R.id.driver_profit);
            useCarTime = mLayout.findViewById(R.id.driver_waiting_order_use_car_time);
            useCarType = mLayout.findViewById(R.id.driver_waiting_order_use_car_type);
            startInfo = mLayout.findViewById(R.id.driver_waiting_order_start_info);
            endInfo = mLayout.findViewById(R.id.driver_waiting_order_end_info);
            orderTime = mLayout.findViewById(R.id.driver_waiting_order_time);
            orderDistance = mLayout.findViewById(R.id.driver_waiting_order_distance);
            driverOrderRouteInfo = mLayout.findViewById(R.id.driver_order_route_info);
            // 箭头初始化，修改箭头颜色
            driverArrow = mLayout.findViewById(R.id.driver_arrow);
            driverArrow.setColorFilter(context.getResources().getColor(R.color.perfect_orange));
            // 按钮初始化
            btnCancel = mLayout.findViewById(R.id.driver_cancel_order_btn);
            btnCancel.setClickable(true);
            btnConfirm = mLayout.findViewById(R.id.driver_confirm_order_btn);
            changeOrder = mLayout.findViewById(R.id.driver_change_order);
            changeOrder.setClickable(true);
        }

        // 设置所有文本框
        public Builder setTextView(OrderDTO order) {
            // 设置订单id
            Double distance = Double.valueOf(order.getDistanceToDriver());
            if (distance < 100) {
                distanceToDriverTv.setText("距您" + order.getDistanceToDriver() + "米");
            } else if (distance < 1000) {
                // 保存公里数
                DecimalFormat df = new DecimalFormat("#.0");
                String distanceStr = df.format(distance / 1000);
                distanceToDriverTv.setText("距您0" + distanceStr + "公里");
            } else {
                // 保存公里数
                DecimalFormat df = new DecimalFormat("#.0");
                String distanceStr = df.format(distance / 1000);
                distanceToDriverTv.setText("距您" + distanceStr + "公里");
            }
            useCarTime.setText("用车时间: " + TimeUtil.DateToString(order.getUseCarTime()));
            useCarType.setText(order.getUseCarType());
            startInfo.setText(order.getStartInfo());
            endInfo.setText(order.getEndInfo());
            orderTime.setText("预计全程时间:" + order.getTime() + "分钟");
            orderDistance.setText("预计全程里程:" + order.getDistance() + "公里");
            driverProfitTv.setText("￥收益" + order.getCost() + "元");
            return this;
        }

        // 设置取消按钮文字和监听
        public Builder setButtonCancel(View.OnClickListener listener) {
            mButtonCancelClickListener = listener;
            return this;
        }

        // 设置确认按钮文字和监听
        public Builder setButtonConfirm(View.OnClickListener listener) {
            mButtonConfirmClickListener = listener;
            return this;
        }

        // 设置换订单按钮文字和监听
        public Builder setButtonChangeOrder(View.OnClickListener listener) {
            changeOrderOnClickListener = listener;
            return this;
        }

        // 设置换订单按钮文字和监听
        public Builder setRouteInfoBtn(View.OnClickListener listener) {
            routeInfoOnClickListener = listener;
            return this;
        }

        // 创建对话框
        public ReceiveOrderDialog create() {
            btnCancel.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    mButtonCancelClickListener.onClick(view);
                }
            });

            btnConfirm.setOnClickListener(new android.view.View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    mButtonConfirmClickListener.onClick(view);
                }
            });

            changeOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    changeOrderOnClickListener.onClick(view);
                }
            });

            driverOrderRouteInfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    routeInfoOnClickListener.onClick(view);
                }
            });

            mDialog.setContentView(mLayout);
            mDialog.setCancelable(true);
            mDialog.setCanceledOnTouchOutside(false);
            return mDialog;
        }
    }
}
